package cn.andy.android.crm.fragment;

import cn.andy.android.crm.R;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.Fragment;
import android.text.AlteredCharSequence;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

public class MapDemoFragment extends Fragment implements LocationListener {
	private final static String TAG = "MapDemoFragment";
	private LocationManager lm;

	private TextView tvlocation;
	private Button btnlocate;
	private View view;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		view = inflater.inflate(R.layout.fragment_mapdemo, container, false);

		// 与Location_service建立连接
		lm = (LocationManager) getActivity().getSystemService(
				Context.LOCATION_SERVICE);
		// 注册activity到监听队列中
		lm.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1l, 1l, this);
		// lm.getProvider(LocationManager.GPS_PROVIDER).requiresNetwork();
		Log.d(TAG, "got location");

		tvlocation = (TextView) view.findViewById(R.id.mapdemo_location);
		btnlocate = (Button) view.findViewById(R.id.btnlocate);

		setNewInfo();

		btnlocate.setOnClickListener(new Button.OnClickListener() {

			public void onClick(View arg0) {
				setNewInfo();
				Location loc = lm
						.getLastKnownLocation(LocationManager.GPS_PROVIDER);
				if (loc != null) {
					Uri uri = Uri.parse("geo:" + loc.getLatitude() + ","
							+ loc.getLongitude());
					Intent intent = new Intent(Intent.ACTION_VIEW, uri);
					startActivity(intent);
				}
			}
		});

		return view;
	}

	// Button btn;

	// Location发生变化时会调用这个
	public void onLocationChanged(Location location) {
		Log.d(TAG, "location: " + location);
		setNewInfo();
	}

	// 关闭GPS卫星会调用这个
	public void onProviderDisabled(String provider) {
		Log.d(TAG, "provider disable" + provider);
		setNewInfo();
	}

	// 启用GPS卫星会调用这个
	public void onProviderEnabled(String provider) {
		Log.d(TAG, "provider enable");
		setNewInfo();
	}

	public void onStatusChanged(String provider, int status, Bundle extras) {
		Log.d(TAG, "status changed status = " + status);
	}

	private void setNewInfo() {
		boolean isopengps = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
		if (!isopengps) {
			new AlertDialog.Builder(getActivity())
					.setTitle(R.string.message_map_enablelocation)
					.setNegativeButton(R.string.message_map_no,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									dialog.cancel();
								}
							})
					.setPositiveButton(R.string.message_map_yes,
							new DialogInterface.OnClickListener() {

								public void onClick(DialogInterface dialog,
										int which) {
									startActivity(new Intent(
											Settings.ACTION_LOCATION_SOURCE_SETTINGS));
								}
							}).create().show();
		}
		Location loc = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		if (loc != null)
			tvlocation.setText("经度：" + loc.getLongitude() + "\n纬度："
					+ loc.getLatitude() + "\n速度：" + loc.getSpeed() + "\n时间："
					+ loc.getTime());
	}

}
